Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
@wessberg/di-compiler
Advanced tools
A Custom Transformer for Typescript that enables compile-time Dependency Injection
A Custom Transformer for Typescript that enables compile-time Dependency Injection
This is a CustomTransformer
for Typescript that enables you to use the DI library.
This has been implemented as a TypeScript Custom Transformer in order to be so low-level that it can be used as the underlying implementation in anything you want, whether it be directly with Typescript's Compiler APIs, Webpack loaders, Rollup plugins, or something else.
Bubbles Twitter: @usebubbles | Christopher Blanchard | Ideal Postcodes | Xerox | Trent Raymond |
$ npm install @wessberg/di-compiler
$ yarn add @wessberg/di-compiler
$ pnpm add @wessberg/di-compiler
@wessberg/di-compiler
depends on typescript
, so you need to manually install this as well.
Since this is a Custom Transformer, it can be used practically anywhere you use TypeScript.
The most obvious place would be to use it directly with Typescript's compiler APIs:
There's several ways to do this, but here's a simple example:
import {createProgram, getDefaultCompilerOptions, createCompilerHost} from "typescript";
import {di} from "@wessberg/di-compiler";
const compilerOptions = getDefaultCompilerOptions();
const compilerHost = createCompilerHost(compilerOptions);
// Create a Typescript program
const program = createProgram(
["my-file-1.ts", "my-file-2.ts"],
compilerOptions,
compilerHost
);
// Transform the SourceFiles within the program, and pass them through the 'di' transformer
program.emit(undefined, undefined, undefined, undefined, di({program}));
One of the simplest ways to use DI-compiler is with ts-node
:
node -r @wessberg/di-compiler/register
You can also do it programmatically. Here's an example using CommonJS:
const {di} = require("@wessberg/rollup-plugin-ts");
require("ts-node").register({
transformers: program => di({program})
});
import {createProgram, getDefaultCompilerOptions, createCompilerHost} from "typescript";
import {di} from "@wessberg/di-compiler";
const compilerOptions = getDefaultCompilerOptions();
const compilerHost = createCompilerHost(compilerOptions);
// Create a Typescript program
const program = createProgram(
["my-file-1.ts", "my-file-2.ts"],
compilerOptions,
compilerHost
);
// Transform the SourceFiles within the program, and pass them through the 'di' transformer
program.emit(undefined, undefined, undefined, undefined, di({program}));
There are two popular TypeScript plugins for Rollup that support Custom Transformers:
import ts from "@wessberg/rollup-plugin-ts";
import {di} from "@wessberg/di-compiler";
export default {
input: "...",
output: [
/* ... */
],
plugins: [
ts({
transformers: [di]
})
]
};
import ts from "rollup-plugin-typescript2";
import {di} from "@wessberg/di-compiler";
export default {
input: "...",
output: [
/* ... */
],
plugins: [
ts({
transformers: [service => di({program: service.getProgram()})]
})
]
};
There are two popular TypeScript loaders for Webpack that support Custom Transformers:
import {di} from "@wessberg/di-compiler";
const config = {
// ...
module: {
rules: [
{
// Match .mjs, .js, .jsx, and .tsx files
test: /(\.mjs)|(\.[jt]sx?)$/,
loader: "awesome-typescript-loader",
options: {
// ...
getCustomTransformers: program => di({program})
}
}
]
}
// ...
};
import {di} from "@wessberg/di";
const config = {
// ...
module: {
rules: [
{
// Match .mjs, .js, .jsx, and .tsx files
test: /(\.mjs)|(\.[jt]sx?)$/,
loader: "ts-loader",
options: {
// ...
getCustomTransformers: program => di({program})
}
}
]
}
// ...
};
You can also use DI-compiler with the ava
test runner
with the require
property in the ava
configuration:
{
// Other options...
extensions: ["ts"],
require: ["@wessberg/di-compiler/register"]
}
You can provide options to the di
Custom Transformer to configure its behavior:
Option | Description |
---|---|
program | A full TypeScript program (required). |
typescript (optional) | If given, the TypeScript version to use internally for all operations. |
Do you want to contribute? Awesome! Please follow these recommendations.
Frederik Wessberg Twitter: @FredWessberg Github: @wessberg Lead Developer |
First, classes that are discovered as part of your Typescript program/bundle will be parsed for their constructor argument types and positions. Then, instances of the DIContainer will be discovered and their expressions will be upgraded. For example, an expression such as:
import {DIContainer} from "@wessberg/di";
import {MyInterface} from "./my-interface";
import {MyImplementation} from "./my-implementation";
const container = new DIContainer();
container.registerSingleton<MyInterface, MyImplementation>();
Will be compiled into:
// ...
container.registerSingleton(undefined, {
identifier: `MyInterface`,
implementation: MyImplementation
});
MIT © Frederik Wessberg (@FredWessberg) (Website)
FAQs
A Custom Transformer for Typescript that enables compile-time Dependency Injection
The npm package @wessberg/di-compiler receives a total of 453 weekly downloads. As such, @wessberg/di-compiler popularity was classified as not popular.
We found that @wessberg/di-compiler demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.